package mvp.com.bbc.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import mvp.com.bbc.MainActivity;
import mvp.com.bbc.R;
import mvp.com.bbc.appContact.AppContact;
import mvp.com.bbc.base.BaseFragment;
import mvp.com.bbc.bean.LoginBean;
import mvp.com.bbc.di.component.DaggerLoginComponent;
import mvp.com.bbc.di.modules.LoginModules;
import mvp.com.bbc.ui.presenter.LoginPresenter;
import mvp.com.bbc.utils.ShareP;

/**
 * Created by Administrator on 2017/9/29.
 */

public class LoginFragment extends BaseFragment implements AppContact.LoginView {

    private EditText mInputU, mInputP;
    private Button mBtnLogin;
    private LoginPresenter mPresemter;
    private String mUserPhon;
    private String mUserPws;

    public static BaseFragment newInstance() {
        return new LoginFragment();
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mInputU = (EditText) view.findViewById(R.id.et_input_u);
        mInputP = (EditText) view.findViewById(R.id.et_input_p);
        mBtnLogin = (Button) view.findViewById(R.id.btn_login);
        mPresemter = DaggerLoginComponent.builder()
                .loginModules(new LoginModules(this))
                .build()
                .getPresemter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }


    @Override
    public void login(final LoginBean loginBean) {
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUserPhon = mInputU.getText().toString().trim();
                mUserPws = mInputP.getText().toString().trim();
                if (mUserPhon.isEmpty() || mUserPws.isEmpty()) {
                    Toast.makeText(getContext(), "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    //登录成功
                    mPresemter.loginPresenter(mUserPhon, mUserPws);
                    getContext().startActivity(new Intent(getContext(), MainActivity.class));
                    //保存信息 并进行界面之间的转换
                    ShareP.setParam(getContext(), "userid", loginBean.getAu().getUserid());
                    ShareP.setParam(getContext(), "age", loginBean.getAu().getAge());
                    ShareP.setParam(getContext(), "icon", loginBean.getAu().getIcon());
                    ShareP.setParam(getContext(), "username", loginBean.getAu().getNicename());
                    ShareP.setParam(getContext(), "phone", loginBean.getAu().getPhone());
                    ShareP.setParam(getContext(), "rolename", loginBean.getAu().getRolename());
                    ShareP.setParam(getContext(), "sex", loginBean.getAu().getSex());
                    ShareP.setParam(getContext(), "password", loginBean.getAu().getPassword());
                }
            }
        });
    }
}
